{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# L1范数\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的核心目的是计算两个向量 $a$ 和 $b$ 的 $L_1$ 范数（曼哈顿距离范数）。在数学上，$L_1$ 范数定义为向量中所有元素的绝对值之和：\n",
    "\n",
    "$$\n",
    "\\| x \\|_1 = \\sum_{i=1}^{n} |x_i|\n",
    "$$\n",
    "\n",
    "其中，$x$ 是 $n$ 维向量，$|x_i|$ 表示向量分量的绝对值。\n",
    "\n",
    "---\n",
    "\n",
    "### **步骤解析**\n",
    "\n",
    "1. **初始化向量**：\n",
    "   - 向量 $a$ 被定义为：\n",
    "    $$\n",
    "     a = \\begin{bmatrix} 3 \\\\ -4 \\end{bmatrix}\n",
    "     $$\n",
    "   - 向量 $b$ 被定义为：\n",
    "    $$\n",
    "     b = \\begin{bmatrix} 0 \\\\ 5 \\end{bmatrix}\n",
    "     $$\n",
    "\n",
    "2. **计算 $L_1$ 范数**：\n",
    "   - 使用 `np.linalg.norm(a, ord=1)` 计算 $a$ 的 $L_1$ 范数：\n",
    "    $$\n",
    "     \\| a \\|_1 = |3| + |-4| = 3 + 4 = 7\n",
    "     $$\n",
    "   - 另一种计算方法是 `np.sum(np.abs(a))`，其数学表达式为：\n",
    "    $$\n",
    "     \\sum_{i=1}^{2} |a_i| = |3| + |-4| = 7\n",
    "     $$\n",
    "   - 对于 $b$，使用 `np.linalg.norm(b, ord=1)` 计算：\n",
    "    $$\n",
    "     \\| b \\|_1 = |0| + |5| = 0 + 5 = 5\n",
    "     $$\n",
    "   - 同样， `np.sum(np.abs(b))` 计算：\n",
    "    $$\n",
    "     \\sum_{i=1}^{2} |b_i| = |0| + |5| = 5\n",
    "     $$\n",
    "\n",
    "---\n",
    "\n",
    "### **总结**\n",
    "这段代码利用 NumPy 计算了向量 $L_1$ 范数，也可以直接使用绝对值求和的方式验证结果。数学上，$L_1$ 范数在很多领域（如优化问题、稀疏信号处理）中广泛应用，因为它在某些情况下比欧几里得范数更具鲁棒性（例如，$L_1$ 范数在异常点较多的数据中比 $L_2$ 范数更稳定）。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "195f5a7b-2193-469d-b830-57dcd6848916",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81b42961-2766-4773-b844-d34fcae832ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4e6055b-a926-4fb4-8d49-836f908cab7a",
   "metadata": {},
   "source": [
    "## 定义向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67fd39bf-3ce7-4da1-92ef-9132ae01272b",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([3, -4])\n",
    "b = np.array([0, 5])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "504c4d4d-f767-451f-a9db-714725ee2201",
   "metadata": {},
   "source": [
    "## 计算 L1 范数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e940e4e-5cca-4e6a-8cf0-fa95d5f25226",
   "metadata": {},
   "outputs": [],
   "source": [
    "L1_a = np.linalg.norm(a, ord=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "674b9598-babe-439b-ad38-17ca06d7c58e",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(np.abs(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ecb177e-0348-447c-a679-17129926e75b",
   "metadata": {},
   "outputs": [],
   "source": [
    "L1_b = np.linalg.norm(b, ord=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ef1cde1-1202-4798-a265-220d4f29cdc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(np.abs(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc337583-c09d-4c5c-8af4-1f11950515d4",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
